package cn.stylefeng.guns.modular.time.controller;

import cn.stylefeng.guns.modular.time.entity.Tasks;
import cn.stylefeng.guns.modular.time.model.request.TasksRequest;
import cn.stylefeng.guns.modular.time.service.TasksService;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

@RestController
@ApiResource(name = "任务管理")
public class TasksController {

    @Resource
    private TasksService tasksService;

    @GetResource(name = "分页查询任务列表", path = "/tasks/page")
    public ResponseData<PageResult<Tasks>> findPage(TasksRequest tasksRequest) {
        return new SuccessResponseData<>(tasksService.findPage(tasksRequest));
    }

    @PostResource(name = "添加任务", path = "/tasks/add")
    public ResponseData<Void> add(@RequestBody @Validated TasksRequest tasksRequest) {
        tasksService.add(tasksRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "编辑任务", path = "/tasks/edit")
    public ResponseData<Void> edit(@RequestBody @Validated TasksRequest tasksRequest) {
        tasksService.edit(tasksRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "删除任务", path = "/tasks/delete")
    public ResponseData<Void> delete(@RequestBody TasksRequest tasksRequest) {
        tasksService.delete(tasksRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "更新任务状态", path = "/tasks/updateStatus")
    public ResponseData<Void> updateStatus(@RequestBody TasksRequest tasksRequest) {
        tasksService.updateStatus(tasksRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "更新任务置顶状态", path = "/tasks/updatePinned")
    public ResponseData<Void> updatePinned(@RequestBody TasksRequest tasksRequest) {
        Tasks tasks = tasksService.getById(tasksRequest.getTaskId());
        if (tasks != null) {
            tasks.setPinned(tasksRequest.getPinned());
            tasksService.updateById(tasks);
        }
        return new SuccessResponseData<>();
    }
}
